package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.pojo.PageResult;
import cn.itcast.hotel.pojo.RequestParams;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SpringBootTest
public class t {

    @Autowired
    private IHotelService hotelService;

    @Autowired
    private RestHighLevelClient client;

//    @Test
//    void createIndex() throws IOException {
//
//        CreateIndexRequest request = new CreateIndexRequest("hotee");
//        request.source(MAPPING_TEMPLATE, XContentType.JSON);
//        client.indices().create(request, RequestOptions.DEFAULT);
//    }

    @AfterEach
    public void close() throws IOException {
        client.close();
    }

    @Test
    void addData() throws IOException {
        List<Hotel> list = hotelService.list();

        List<HotelDoc> hotelDocs = list.stream().map(HotelDoc::new).collect(Collectors.toList());

        IndexRequest request = null;
        for (HotelDoc hotelDoc : hotelDocs) {
            request = new IndexRequest("hotee").id(hotelDoc.getId().toString());
            String hotelJson = JSON.toJSONString(hotelDoc);
            request.source(hotelJson, XContentType.JSON);
            client.index(request, RequestOptions.DEFAULT);
        }

    }

    @Test
    public void agg() throws IOException {

        SearchRequest request = new SearchRequest("hotel");
        request.source().size(0);
        request.source().aggregation(AggregationBuilders.terms("brandAgg").size(20).field("brand"));

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        Aggregations aggregations = response.getAggregations();
        Terms brandAgg = aggregations.get("brandAgg");
        List<? extends Terms.Bucket> buckets = brandAgg.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            System.out.println(bucket.getKey());
        }
    }

    @Test
    void aa() {
        RequestParams params = new RequestParams();
        params.setSize(5);
        params.setPage(1);
        Map<String, List<String>> filters = hotelService.filters(params);
        System.out.println(filters);
    }

    @Test
    void aax() {
        RequestParams params = new RequestParams();
        params.setSize(5);
        params.setPage(1);
        params.setBrand("希尔顿");
        PageResult search = hotelService.search(params);
//        System.out.println(search);
    }


    @Test
    void ff() throws IOException {
        List<Hotel> list = hotelService.list();

        List<HotelDoc> hotelDocs = list.stream().map(HotelDoc::new).collect(Collectors.toList());
        BulkRequest bulkRequest = new BulkRequest();

        for (HotelDoc hotelDoc : hotelDocs) {
            String hotelDocJson = JSON.toJSONString(hotelDoc);


            bulkRequest.add(new IndexRequest("hotel").source(hotelDocJson, XContentType.JSON).id(hotelDoc.getId().toString()));
        }

        client.bulk(bulkRequest, RequestOptions.DEFAULT);

    }

    @Test
    void mm() throws IOException {

        SearchRequest request = new SearchRequest("hotel");

        request.source()
                .suggest(
                        new SuggestBuilder().addSuggestion(
                                "ssss",
                                SuggestBuilders
                                        .completionSuggestion("suggestion")
                                        .size(10)
                                        .prefix("hz ")
                                        .skipDuplicates(true)
                        ));

        SearchResponse search = client.search(request, RequestOptions.DEFAULT);
//        System.out.println(search);

        Suggest suggest = search.getSuggest();
        CompletionSuggestion suggestion = suggest.getSuggestion("ssss");
        List<CompletionSuggestion.Entry.Option> list = suggestion.getOptions();
        for (CompletionSuggestion.Entry.Option option : list) {
            String s = option.getText().toString();
            System.out.println(s);
        }

//        System.out.println(suggest);
//        SearchHits hits = search.getHits();
//        SearchHit[] hitsHits = hits.getHits();
//        for (SearchHit documentFields : hitsHits) {
//            String sourceAsString = documentFields.getSourceAsString();
//            HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class);
//            System.out.println(hotelDoc);
//        }
    }

    @Test
    void hhhd() {
        List<String> s = hotelService.getSuggestion("s");
        System.out.println(s);
    }
}
